import {count} from '../../../assets/js/help.js'
import echarts from 'echarts'

export default {
  data() {
    return{
      incomeFormChartObj: null, //收入构成obj
      incomeTrendChartObj: null, //收入趋势obj
      incomeFormChartOption: {
        title : {
          text: '收入构成',
          x:'left',
          textStyle:{
            fontSize: 15,
            fontWeight: 'lighter'
          },
        },
        "tooltip": { "trigger": "item", "formatter": "{a} <br/>{b}: {c} ({d}%)"},
        "color": ["#EA7D5E", "#fd87ab", "#11abff", "#ffdf33", "#968ade", "#5174DE"],
        legend: {
          orient: 'vertical',
          top: '10',
          right: 'right',
          data: ['临停','月票','优惠券','共享','预订']
        },
        "series": [{
          "name": "金额组成", "type": "pie", "radius": ["38%", "50%"],
          "avoidLabelOverlap": false,
          "label": { 
            "normal": { "formatter": "{d}%" }, 
            "emphasis": { "show": true, "textStyle": { "fontSize": "12", "fontWeight": "bold" } } 
            },
            "labelLine": { "normal": { "show": true } },
            "data": [
              { "value": 0, "labelLine": { "length": 0 }, "name": "临停" }, 
              { "value": 0, "labelLine": { "length": 10 }, "name": "月票" }, 
              { "value": 0, "labelLine": { "length": 20 }, "name": "优惠券" }, 
              { "value": 0, "labelLine": { "length": 30 }, "name": "共享" }, 
              { "value": 0, "labelLine": { "length": 40 }, "name": "预订" }
            ]
        }]
      },
      incomeTrendChartOption:  {
        "title": {
          "text": '收入趋势',
          "x":'left',
          "textStyle":{
            fontSize: 15,
            fontWeight: 'lighter'
          },
        },
        "grid":{
            "containLabel":true
        },
        "legend":{
            "data":[
                "收入：",
                "同比：",
                "环比："
            ],
            "bottom":5
        },
        "color":[
            "#E1D8D4",
            "#5174DE",
            "#EB9643",
            "#ffdf33",
            "#968ade"
        ],
        "tooltip":{
            "trigger":"axis",
            "formatter":{
                "_custom":{
                    "type":"function",
                    "display":"<span>ƒ</span> formatter(tipData)"
                }
            },
            "axisPointer":{
                "label":{
                    "backgroundColor":"#283b56"
                }
            }
        },
        "xAxis":[
            {
                "type":"category",
                "data":[
                    "11-12",
                    "11-13",
                    "11-14",
                    "11-15",
                    "11-16",
                    "11-17",
                    "11-18"
                ]
            }
        ],
        "yAxis":[
            {
                "type":"value",
                "axisLabel":{
                    "interval":0,
                    "margin":10,
                    "formatter":{
                        "_custom":{
                            "type":"function",
                            "display":"<span>ƒ</span> formatter(value, index)"
                        }
                    }
                }
            },
            {
                "axisLabel":{
                    "formatter":"{value}%",
                    "interval":0,
                    "margin":10
                },
                "splitLine":{
                    "show":false
                }
            }
        ],
        "series":[
            {
                "name":"收入：",
                "data":[
                    "335.00",
                    "123.00",
                    "34.00",
                    "341.00",
                    "286.10",
                    0,
                    0
                ],
                "type":"bar"
            },
            {
                "yAxisIndex":1,
                "name":"同比：",
                "data":[
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0
                ],
                "type":"line"
            },
            {
                "yAxisIndex":1,
                "name":"环比：",
                "data":[
                    0,
                    -63.28,
                    -72.36,
                    902.94,
                    -16.1,
                    -100,
                    0
                ],
                "type":"line"
            }
        ]
      },
      calendarList: [], //日历列表
      calendarListObj: [], //日历列表对象
      year: '', //年份
      month: '', //月份
    }
  },
  mounted(){
    this.incomeFormChartObj = echarts.init(document.getElementById('income-form-echart'));
    this.incomeTrendChartObj = echarts.init(document.getElementById('income-trend-echart'));
    this.renderIncomeForm()
    this.renderIncomeTrend()
    this.calculateCalendar() //计算日历
  },
  methods: {
    renderIncomeForm(){ //收入构成
      let _this = this;
      _this.incomeFormChartObj.setOption(_this.incomeFormChartOption);
    },
    renderIncomeTrend(){ //收入趋势
      let _this = this;
      _this.incomeTrendChartObj.setOption(_this.incomeTrendChartOption);
    },
    confirmSearch(){

    },
    getList(){

    },
    handleSearchChange(item) {
      switch (item.name) {
        case 'reportDimension':
          this.changeReportDimension(item);
          break;
      }
    },
    changeReportDimension(item) {
      let value = item.value,
        ThisSearchItem = this.searchList[0];
      var month = new Date(moment().subtract(1, 'months').format('YYYY-MM-DD 00:00:00'));
      var year = new Date(moment().format('YYYY-MM-DD 00:00:00'));
      switch (value) {
        case 'month':
          ThisSearchItem.style = {
            width: '185px'
          };
          ThisSearchItem.flag = 'month';
          ThisSearchItem.format = 'yyyy-MM';
          ThisSearchItem.value = month;
          break;
        case 'year':
          ThisSearchItem.style = {
            width: '125px'
          };
          ThisSearchItem.flag = 'year';
          ThisSearchItem.format = 'yyyy';
          ThisSearchItem.value = year;
          break;
      }
    },
    //计算日历
    calculateCalendar(){
        this.calendarList = [];
        this.calendarListObj = [];
        let nowdays = new Date();
        let year = nowdays.getFullYear();
        let month = nowdays.getMonth();
        if(month==0)
        {
            month=11;
            year=year-1;
        }
        this.year = year
        this.month = month
        // 上个月最后一天
        let curlastDay = (new Date(year, month, 0)).getDate();
        for(let i = curlastDay; i>0; i--){
            let obj = {};
            obj.day = i<10 ? ('0' + i) : i; 
            obj.money = '0';
            this.calendarList.unshift(obj);
        }
        let myDate = new Date(year, month-1, 0); 
        //上上个月最后一天的日期
        let lastMonthDay = myDate.getDate()
        //上月第一天日期对象：
        let date=new Date(nowdays.setDate(1))
        //上月第一天星期几:
        let weekday=date.getDay();
        for(let i = 0; i<weekday; i++){
            let obj = {}
            obj.day = lastMonthDay - i;
            obj.money = '-';
            this.calendarList.unshift(obj);
        }
        let tempArr = []
        for(let i = 0,j=0; i<this.calendarList.length; i++){
            let tempObj = this.calendarList[i]
            if((i+1)%7==0){
                tempArr.push(tempObj)
                this.calendarListObj.push(tempArr)
                tempArr = []
                continue;
            }
            tempArr.push(tempObj)
        }
        this.calendarListObj.push(tempArr)
    },
  }
}